﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XHCTMS.VIEW
{
    public partial class Fraction : System.Web.UI.Page
    {
        string[] parasValue = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["page"] = 1;
                parasValue = new string[] { };
                Session["maxPage"] = new BLL.Fraction().MaxPage(parasValue);
                InfoBind(new BLL.Fraction().Search(parasValue), int.Parse(Session["maxPage"].ToString()));
                Session["ArraryStr"] = parasValue;

                ddlClassName.DataSource = new BLL.Class().GetClassName();
                ddlClassName.DataTextField = "class_name";
                ddlClassName.DataValueField = "class_id";
                ddlClassName.DataBind();
                ddlClassName.Items.Insert(0, new ListItem("", ""));

                ddlTeaTeaName.DataSource = ddlClassTeaName.DataSource = new BLL.User().GetUserName();
                ddlTeaTeaName.DataTextField = ddlClassTeaName.DataTextField = "user_name";
                ddlTeaTeaName.DataValueField = ddlClassTeaName.DataValueField = "user_id";
                ddlTeaTeaName.DataBind();
                ddlClassTeaName.DataBind();
                ddlTeaTeaName.Items.Insert(0, new ListItem("", ""));
                ddlClassTeaName.Items.Insert(0, new ListItem("", ""));


                ddlSubName.DataSource = new BLL.Sub().GetSubName();
                ddlSubName.DataTextField = "sub_name";
                ddlSubName.DataValueField = "sub_id";
                ddlSubName.DataBind();
                ddlSubName.Items.Insert(0, new ListItem("", ""));


            }

        }

        public void InfoBind(DataTable dt, int maxPage)
        {
            List<string> pageList = new List<string> { };
            for (int i = 1; i <= maxPage; i++)
            {
                pageList.Add(i.ToString() + "/" + maxPage.ToString());
            }
            this.ddlPage.DataSource = pageList;
            this.ddlPage.DataBind();
            this.ddlPage.SelectedIndex = int.Parse(Session["page"].ToString()) - 1;
            this.infolist.DataSource = dt;

            this.infolist.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            parasValue = new string[] { ddlClassName.SelectedItem.Text,ddlClassTeaName.SelectedItem.Text,
            ddlTeaTeaName.SelectedItem.Text,ddlSubName.SelectedItem.Text,txtTeaLesson.Text.Trim()};
            try
            {
                int maxpage = new BLL.Fraction().MaxPage(parasValue);
                InfoBind(new BLL.Fraction().Search(parasValue), maxpage);
                Session["ArraryStr"] = parasValue;
                Session["page"] = 1;
                Session["maxPage"] = maxpage;

            }
            catch
            {
                this.infolist.DataSource = null;
                this.infolist.DataBind();
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            InfoBind(new BLL.Fraction().Search(parasValue), int.Parse(Session["MaxPage"].ToString()));
            Session["page"] = 1;
            this.ddlPage.SelectedIndex = 0; 
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int count = int.Parse(Session["page"].ToString()) - 1;
            if (count > 0)
            {
                InfoBind(new BLL.Fraction().Search(parasValue, count), int.Parse(Session["maxPage"].ToString()));
                Session["page"] = count;
                this.ddlPage.SelectedIndex = count - 1;
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int page = int.Parse(Session["page"].ToString()) + 1;
            if (page <= int.Parse(Session["maxPage"].ToString()))
            {
                InfoBind(new BLL.Fraction().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));
                Session["page"] = page;
                this.ddlPage.SelectedIndex = page - 1;
            }
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int maxPage = int.Parse(Session["maxPage"].ToString());
            InfoBind(new BLL.Fraction().Search(parasValue, maxPage), maxPage);
            Session["page"] = maxPage;
            this.ddlPage.SelectedIndex = maxPage - 1;   
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int page = int.Parse(this.ddlPage.SelectedValue.Split('/')[0]);
            InfoBind(new BLL.Fraction().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));
            Session["page"] = page;
            this.ddlPage.SelectedIndex = page - 1;
        }
    }
}